import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pandas as pd
import tushare as ts
from utils.confutils import ConfUtil
import time
from tqdm import tqdm
import datetime

# 提取token
token = ConfUtil().get_tokens('tushare')
ts.set_token(token)
# 提取所有股票代码（含退市股票的代码)
stock_basic_file = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\Data\\RawData\\stock_basic.feather"
stock_list = pd.read_feather(stock_basic_file,columns=['ts_code']).drop_duplicates()['ts_code']
print(f"一共{len(stock_list)}个股票")

# 循环下载股票日线数据（复权方式为前复权）,20100101至今
today_str = datetime.datetime.now().strftime('%Y%m%d')
df_full = []
print('开始下载股票日线数据...')
for ts_code in tqdm(stock_list):
    print(f'开始下载股票:{ts_code}日线数据...')
    is_fail = True
    while 1==1:
        try:
            df = ts.pro_bar(ts_code=f"{ts_code}", 
                            adj='qfq', 
                            start_date='20100101', 
                            end_date=today_str,
                            fields='ts_code,trade_date,open,close,high,low,pre_close,change,vol,amount')
            break
        except:
            time.sleep(5)
            continue
    print(f'{ts_code}日线数据下载成功...')
    
    df_full.append(df)
df_res = pd.concat(df_full,axis=0)
print(f'日线数据信息:\n {df.info()}')
print(df_res.head(5))

# 存到..\Data\RawData\stock_daily_bar.feather文件中
tofile = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\Data\\RawData\\stock_dailybar.feather"
df_res.to_feather(tofile)


